﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ActionResultSamples.Models;

namespace ActionResultSamples.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View("NotTheAboutView");
        }

        public ActionResult Redirect()
        {
            return Redirect("http://www.wrox.com");
        }

        public ActionResult RedirectToActionSample()
        {
            return RedirectToAction("Redir", "Home", new
            {
                id = 12,
                name = "Wrox"
            });
        }

        public ActionResult RedirectToRouteSample()
        {
            RouteValueDictionary dictionary = new RouteValueDictionary();
            dictionary["action"] = "Redir";
            dictionary["controller"] = "Home";
            dictionary["id"] = 12;
            dictionary["name"] = "Wrox";

            return RedirectToRoute(dictionary);
        }

        public ActionResult Redir(int id, string name)
        {
            ViewData["Title"] = "Redir Page";
            ViewData["ID"] = id.ToString();
            ViewData["Name"] = name;

            return View("RedirPage");
        }


        public ActionResult JsonSample()
        {
            Models.User loggedUser = new User()
            {
                Username = "MVCDeveloper",
                Email = "mvcdev@wrox.com",
                HomeAddress = new Address()
                {
                    Country = "Italy",
                    City = "Milano",
                    Street = "Viale Monza"
                }
            };

            return Json(loggedUser, "text/plain");
        }

        public ActionResult ContentSample()
        {
            return Content("Italy|Simone|Iran|Keyvan", "text/plain");
        }

        public ActionResult File()
        {
            //Stream str = new FileStream("Content/WroxPicture.gif", FileMode.Open);
            //return File(str, "image/gif", "picture.gif");
            return File("~/Content/WroxPicture.gif", "image/gif", "picture.gif");
        }

    }
}
